#region File Header
//----------------------------------------------------------------
// Copyright (C) 2002 Stefan Kyntchev
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY 
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR 
// FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------
#endregion File Header

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;


namespace AirWeb.WebControls.BandsControl
{
	/// <summary>
	/// This control renders a table of clickabla bands. Each band has a clickable title and 
	/// contains an iframe, pointing to some site. One band can be set as default and it will 
	/// be opened on load.
	/// </summary>
	[DefaultProperty("Text"),
	ToolboxData("<{0}:BandsControl runat=server></{0}:BandsControl>")]
	public class BandsControl : System.Web.UI.WebControls.WebControl
	{
		private ArrayList m_Bands = new ArrayList();
		private string m_headerStyle;
		public Table m_table = new Table();

		[Bindable(true),
		Category("Appearance"),
		DefaultValue("")]

			/// <summary>
			/// Adds a band to the bands control
			/// </summary>
		public void AddBand(Band i_Band)
		{
			m_Bands.Add(i_Band);
			i_Band.SetParent(this);
		}

		/// <summary>
		/// Contains the css style of the header of the band
		/// </summary>
		public string HeaderStyle
		{
			set
			{
				m_headerStyle = value;
			}

			get
			{
				return m_headerStyle;
			}
		}

		public Table BandsTable
		{
			get
			{
				return m_table;
			}
		}

		/// <summary>
		/// Render this control to the output parameter specified.
		/// </summary>
		/// <param name="output"> The HTML writer to write out to </param>
		protected override void Render(HtmlTextWriter output)
		{
			// Render the client side band header click logic
			output.WriteLine("<script language='javascript'>");
			output.WriteLine("var __active;");
			output.WriteLine("function __onSelectBand(i_object){");
			output.WriteLine("if(__active) __active.nextSibling.style.display = 'none';");
			output.WriteLine("i_object.nextSibling.style.display='block';");
			output.WriteLine("__active = i_object; }");
			output.WriteLine("</script>");

			// Render the band table
			m_table.CellPadding = m_table.CellSpacing = 0;
			m_table.Width = new Unit("100%");
			m_table.ID = "__BandTable";
			
			// Ask the bands to render themselves
			for(int a = 0; a != m_Bands.Count; a++)
			{
				((Band)m_Bands[a]).Render();
			}

			m_table.RenderControl(output);

			// Activate the default band
			output.WriteLine("<script language='javascript'>");
			output.WriteLine("var el = document.getElementById('__defaultBand');");
			output.WriteLine("if(el)	__onSelectBand(el);");
			output.WriteLine("__BandTable.height='100%';");
			output.WriteLine("</script>");
		}
	}
}

